// pages/component/homes/bindalipay/bindalipay.js
var that;
var Util = require('../../../../utils/util.js');
var GlobalData = require('../../../../utils/GlobalData.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputName: "",
    inputNum: "",
    bindArr: []

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {


  },
  onOk: function () {
    var app = getApp();
    that = this
    if (that.data.inputName == "" || that.data.inputName == null || that.data.inputName == undefined) {
      app.onTip("请输入支付宝名字");
      return;
    }
    if (that.data.inputNum == "" || that.data.inputNum == null || that.data.inputNum == undefined) {
      app.onTip("请输入支付宝账号");
      return;
    }
    let url = app.getRequestUrl(app.getConfig().isFormal);
    wx.request({
      url: url,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: Util.json2Form({ method: "bindbankaccount", token: GlobalData.token, "type": 2, "bankname": "支付宝", "truename": that.data.inputName, "account": that.data.inputNum }),
      complete: function (res) {
        let pkg = res.data;
        if (res && pkg && pkg.errcode == 0) {
          // 获取绑定列表信息
          wx.request({
            url: url,
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            method: "POST",
            data: Util.json2Form({ method: "getbankaccountlist", token: GlobalData.token }),
            complete: function (res) {
              let pkg = res.data;
              if (pkg && pkg.errcode == 0) {
                var arr = [];
                arr = pkg.data;
                that.setData({
                  bindArr: arr
                });
              } else {
                app.onTip(pkg.errmsg)
                return;
              }

            }
          })

        } else {
          console.error(pkg.errmsg);
          app.onTip(pkg.errmsg)

        }
      }
    })


  },
  bindInputName: function (e) {
    this.setData({
      inputName: e.detail.value
    })

  },
  bindInputNum: function (e) {
    this.setData({
      inputNum: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})